package com.mk.web.threadpool;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 自定义线程池
 * Created by WangChen on 2018/12/16 10:32.
 */
public class UserThreadPool {

    public static void main(String [] args){
        BlockingQueue queue = new LinkedBlockingQueue(2);
        UserThreadFactory threadFactory1 = new UserThreadFactory("第一机房");
        UserThreadFactory threadFactory2 = new UserThreadFactory("第二机房");
        UserRejectHandler rejectHandler = new UserRejectHandler();
        ThreadPoolExecutor poolExecutor1 =
                new ThreadPoolExecutor(1, 2, 2000,
                        TimeUnit.MILLISECONDS, queue, threadFactory1, rejectHandler);
        ThreadPoolExecutor poolExecutor2 =
                new ThreadPoolExecutor(1, 2, 2000,
                        TimeUnit.MILLISECONDS, queue, threadFactory2, rejectHandler);

        Runnable task = new Task();
        for (int i = 0; i < 200; i++){
            poolExecutor1.execute(task);
            poolExecutor2.execute(task);
        }
    }
}
